<?php
/**
 * 商城首页
 * @copyright  Copyright (c) 2007-2013 ShopNC Inc. (http://www.shopnc.net)
 * @license    http://www.shopnc.net
 * @link       http://www.shopnc.net
 * @since      File available since Release v1.1
 */
defined('InShopNC') or exit('Access Invalid!');

class shopControl extends BaseHomeControl
{
    /**
     * 模型对象
     * @var \searchModel
     */
    protected $_model_search;

    /**
     *
     * @return goodsModel
     */
    protected function getModelGoods(){
        return Model('goods');
    }
    public function indexOp()
    {
        if(IS_MOBILE){
            header("Location: ".WAP_SITE_URL."\n");
            return;
        }
        Language::read('home_index_index');
        Tpl::output('index_sign', 'index');
        //团购专区
        Language::read('member_groupbuy');
        $model_groupbuy = Model('groupbuy');
        $group_list = $model_groupbuy->getGroupbuyCommendedList(array(), null, '', '*', 4);
        Tpl::output('group_list', $group_list);
        //限时折扣
        $model_xianshi_goods = Model('p_xianshi_goods');
        $xianshi_item = $model_xianshi_goods->getXianshiGoodsCommendList(4);
        Tpl::output('xianshi_item', $xianshi_item);
        //板块信息
        $model_web_config = Model('web_config');
        $web_html = $model_web_config->getWebHtml('index');

        //查询书剑系列商品
        $model_goods = $this->getModelGoods();
        // 字段
        $fields = "goods_id,goods_commonid,goods_name,goods_jingle,gc_id,store_id,store_name,goods_price,goods_marketprice,goods_storage,goods_image,goods_freight,goods_salenum,color_id,evaluation_good_star,evaluation_count";
        $order = 'goods_id desc';
        //读取模型对象
        $this->_model_search = Model('search');
        $sjIndexGoods = array();
        $sjIndexAid = array(25, 33, 26, 29,27,34);
        foreach ($sjIndexAid as $a_id) {
            $_attr_good = $this->_model_search->getAttrList(1074, 0, $a_id);
            $attr = current(array_pop($_attr_good));
            $goodList = array();
            if (isset($_attr_good['goodsid_array'])) {
                $condition = array('goods_id' => array('in',$_attr_good['goodsid_array']), 'gc_id' => 1074);
                $goodList = (array)$model_goods->getGoodsListByColorDistinct($condition, $fields, $order, 6);
            }
            $sjIndexGoods[$a_id] = compact('attr', 'goodList');
        }
        $otherGoodIds=array();
        $otherAids=array_keys(array_diff_key($_attr_good['attr_array'][6]['value'],array_flip($sjIndexAid)));
        foreach($otherAids as $a_id){
            $_attr_good = $this->_model_search->getAttrList(1074, 0, $a_id);
            $otherGoodIds=array_merge($otherGoodIds,$_attr_good['goodsid_array']);
        }
        shuffle($otherGoodIds);
        $condition = array('goods_id' => array('in',$otherGoodIds), 'gc_id' => 1074);
        //只显示书剑品牌产品
        $condition['gc_id'] = DEFAULT_SHUJIAN_GC_ID;
        $goodList=(array)$model_goods->getGoodsListByColorDistinct($condition, $fields, $order, 6);
        $attr=array('attr_value_name'=>'其它系列','attr_value_id'=>0);
        $sjIndexGoods[-1]=compact('attr', 'goodList');
        //获取 即将上市产品,上市新品,已售罄产品
        $shopSales=array(
            '即将上市产品<span class="more_shop"><a href="'.urlShop('search','index',array('cate_id'=>1074,'a_id'=>0,'st'=>'n1')).'" target="_blank">更多</a></span>'=>array('type'=>'noGrounding','list'=>$model_goods->getNoGrounding(6)),
            '上市新品<span class="more_shop"><a href="'.urlShop('search','index',array('cate_id'=>1074,'a_id'=>0,'st'=>'n2')).'" target="_blank">更多</a></span>'=>array('type'=>'','list'=>$model_goods->getNewGrounding(6)),
            '已售罄产品<span class="more_shop"><a href="'.urlShop('search','index',array('cate_id'=>1074,'a_id'=>0,'st'=>'n3')).'" target="_blank">更多</a></span>'=>array('type'=>'','list'=>$model_goods->getWhereStorage(6)),
        );

        $index_sale='<ul class="tabs-nav">';
        $saleIndex=0;
        $saleContent=array();
        $_tpl='<li><dl><dt class="goods-name"><a target="_blank" href="{url}" title="{title}">{title}</a></dt>
                      <dd class="goods-thumb">
                          <a target="_blank" href="{url}">
                              <img src="{thumb}" alt="{title}">
                          </a></dd>
                      <dd class="goods-price">{price_name}：<em>￥{price}</em></dd>{other}</dl></li>';
        foreach($shopSales as $itemName=>$item){
            $index_sale.='<li class="'.($saleIndex?'':'tabs-selected').'"><i class="arrow"></i><h3>'.$itemName.'</h3></li>';
            $_content='<div class="tabs-panel sale-goods-list'.($saleIndex?' tabs-hide':'').'" style="height: 250px;"><ul>';
            if($item['list']){
                foreach($item['list'] as $goodItem){
                    $replace=array('{url}'=>'','{title}'=>'','{thumb}'=>'','{price_name}'=>'','{price}','{other}');
                    $replace['{title}']=$goodItem['goods_name'].' '.$goodItem['goods_jingle'];
                    $replace['{thumb}']=thumb($goodItem, 240);
                    $replace['{price_name}']='建议零售价';
                    $replace['{price}']=$goodItem['goods_marketprice'];
                    $replace['{url}']=urlShop('goods','index',isset($goodItem['goods_id'])?array('goods_id'=>$goodItem['goods_id']):array('cm_id'=>$goodItem['goods_commonid']));
                    if($item['type']=='noGrounding'){
                        $replace['{other}']='<dd class="goods-grounding"><em>上市时间：'.date('Y-m-d').'</em></dd>';
                    }
                    $_content.=strtr($_tpl,$replace);
                }
            }
            $_content.='</ul></div>';
            $saleContent[]=$_content;
            $saleIndex++;
        }
        $index_sale.='</ul>'.implode('',$saleContent);
        $web_html['index_sale']=$index_sale;
        Tpl::output('web_html', $web_html);
        Tpl::output('sj_goods', $sjIndexGoods);
        //查询seo信息
        Model('seo')->type('index')->show();
        Tpl::showpage('index');
    }
}